<?php
class regionModel extends Model
{
    /**
     * 生成spid 
     * 
     * @param int $pid 父级ID
     */
    public function get_spid($pid) {
        if (!$pid) {
            return 0; 
        }
        $pspid = $this->where(array('id'=>$pid))->getField('spid');
        if ($pspid) {
            $spid = $pspid . $pid . '|';
        } else {
            $spid = $pid . '|';
        }
        return $spid;
    }
    
    /**
     * 获取分类下面的所有子分类的ID集合
     * 
     * @param int $id
     * @param bool $with_self
     * @return array $array 
     */
    public function get_child_ids($id, $with_self=false,$isLimit=1) {
        $spid = $this->where(array('id'=>$id))->getField('spid');
        $spid = $spid ? $spid .= $id .'|' : $id .'|';
        $map = array('spid'=>array('like', $spid.'%'));
        if($isLimit && !empty($_SESSION[C('user_session_name')]['regionIds'])){
            $map['id'] = array('in',$_SESSION[C('user_session_name')]['regionIds']);
        }
        $id_arr = $this->field('id')->where($map)->select();
        $array = array();
        foreach ($id_arr as $val) {
            $array[] = $val['id'];
        }
        $with_self && $array[] = $id;
        return $array;
    }
    
    /**
     * 检测分类是否存在
     * 
     * @param string $name
     * @param int $pid
     * @param int $id
     * @return bool 
     */
    public function name_exists($name, $pid, $id=0) {
        $where = "name='" . $name . "' AND pid='" . $pid . "' AND id<>'" . $id . "'";
        $result = $this->where($where)->count('id');
        if ($result) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 写入缓存
     */
    public function cate_cache() {
        $region_list = F('region_list');
        if(!$region_list){
            $region_list = array();
            $cate_data = $this->field('id,pid,name,level,spid')->where('status=1')->order('ordid')->select();
            foreach ($cate_data as $val) {             
                $region_list['s'][$val['pid']][] = $val;
                $region_list['l'][$val['id']] = $val;
            }
            F('region_list', $region_list);
        }
        return $region_list;
    }

    /**
     * 更新则删除缓存
     */
    protected function _before_write(&$data) {
        F('region_list', NULL);
    }

    /**
     * 删除也删除缓存
     */
    protected function _after_delete($data, $options) {
        F('region_list', NULL);     
    }
    
    /**
     * 参数 
     *  level : 要取哪个等级的数据
     *  regionId : 当前及别的ID
     *  返回 isd: 或者是当前及别下面的所有ID,或者是当前及别的父ID
     */     
     
     public function getMyRegionId($regionId,$level=0){
        $regionList = $this->cate_cache();
        if(isset($regionList['l'][$regionId])){
            $region = $regionList['l'][$regionId];
            if($level>$region['level']){ // 向下找
                
            }
        }
     }
     
     
	/**
	 * 获得地区名称和id数组
	 */
	public function getRegionNameAndId($pid = 1){
		$data = array();
		$res = D('region')->where(array('pid'=>$pid))->select();
		foreach ($res as $row){
			$data[$row['name']] = $row['id'];
		}
		return $data;
	}
     
     
}